package cn.tedu.charging.order.service.impl;

import cn.tedu.charging.common.pojo.message.CheckResultMessage;
import cn.tedu.charging.common.pojo.message.DelayCheckMessage;
import cn.tedu.charging.order.dao.repository.BillRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillExceptionPO;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import cn.tedu.charging.order.service.ConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private BillRepository billRepository;

    @Override
    public void handlerCheckResult(CheckResultMessage msg) {
        //1.从消息对象解析result
        Boolean result = msg.getResult();
        //判断是否成功
        if(result){
            log.debug("订单:{},设备枪:{},本次自检结果是成功的",
                    msg.getOrderNo(),msg.getGunId());
            //pre2 检查成功订单是否存在
            long count=billRepository.countSuccess(msg.getOrderNo());
            if (count>0){
                log.debug("订单:{},成功订单已存在,本次自检结果是成功的",msg.getOrderNo());
                return;
            }
            //2.存储成功订单数据
            saveBillSuccess(msg);
            //TODO 3组织成功启动订单的消息对象
        }else{
            log.debug("订单:{},设备枪:{},本次自检结果是失败的,失败原因:{}",
                    msg.getOrderNo(),msg.getGunId(),msg.getFailDesc());
            //pre4
            long count=billRepository.countFail(msg.getOrderNo());
            if (count>0){
                log.debug("订单:{},失败订单已存在,本次自检结果是失败的",msg.getOrderNo());
                return;
            }
            //4.存储失败的订单数据
            saveBillFail(msg);
            //TODO 5.组织失败启动订单的消息对象
            //6.修改枪状态 ,调用设备 调用deviceClient实现远程处理
        }
        //TODO 7.发送消息给用户

    }

    private void saveBillFail(CheckResultMessage msg) {
        //1.组织一个ChargingBillFailPO
        ChargingBillFailPO fail=new ChargingBillFailPO();
        //1.1从消息直接获取
        fail.setBillId(msg.getOrderNo());
        fail.setFailDesc(msg.getFailDesc());
        fail.setUserId(msg.getUserId());
        fail.setGunId(msg.getGunId());
        //1.2业务补充的属性或者默认值
        fail.setCreateTime(new Date());
        fail.setUpdateTime(fail.getCreateTime());
        fail.setDeleted(0);
        //2.调用仓储层,执行新增失败订单的方法写入数据库
        billRepository.saveFailBill(fail);
    }

    private void saveBillSuccess(CheckResultMessage msg) {
        //1.组织一个ChargingBillSuccessPO
        ChargingBillSuccessPO success=new ChargingBillSuccessPO();
        //1.1从消息获取补充字段
        success.setBillId(msg.getOrderNo());
        success.setUserId(msg.getUserId());
        success.setGunId(msg.getGunId());
        //当前启动阶段 无法补充属性charging_capacity(充电度数) charging_duration(充电时长) charging_end_time
        //支付完了才知道的属性pay_amount pay_time pay_channel
        //1.2业务补充计算的 bill_status 1正在充电 2充电完成 3充电异常 4欠费
        success.setBillStatus(1);
        success.setChargingStartTime(new Date());
        success.setUpdateTime(success.getChargingStartTime());
        success.setCreateTime(success.getChargingStartTime());
        success.setDeleted(0);
        //2.调用仓储层,执行新增成功订单的方法写入数据库
        billRepository.saveSuccessBill(success);
    }

    @Override
    public void handleCheckNoRes(DelayCheckMessage msg) {

    }

}
